﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main.SystemManage
{
    public partial class FrmProductionLine : UniRlv.UI.Main.FrmBase
    {
        BindingList<ProductionLine> details = new BindingList<ProductionLine>();
        public FrmProductionLine()
        {
            InitializeComponent();
        }

        private void FrmProductionLine_Load(object sender, EventArgs e)
        {
            #region 初始化工具栏

            toolBtnModify.Visible = true;
            toolBtnAdd.Visible = true;
            toolBtnDelete.Visible = true;

            toolBtnAdd.Click += toolBtnAdd_Click;
            toolBtnDelete.Click += toolBtnDelete_Click;
            toolBtnModify.Click += toolBtnModify_Click;

            ToolStripButton btnEditLineDeivce = AddToolButton("packageEdit", "编辑产线设备");
            btnEditLineDeivce.Click += btnEditLineDevice_Click;

            #endregion

            #region 初始化DataGridView和数据源

            UIHelper.InitDataGridView(dgvDetails);

            dgvDetails.DataSource = details;

            Wait();
            string msg = string.Empty;
            List<ProductionLine> infos = DALOpr.QueryAll<ProductionLine>(ref msg);

            if (!string.IsNullOrEmpty(msg))
            {
                ShowErrorMessageBox("出现错误", msg);
            }
            else
            {
                if (infos == null || infos.Count == 0)
                {
                    ShowWarnningMessageBox("提示", "缺少设备信息");
                }
                else
                {
                    foreach (var item in infos)
                    {
                        details.Add(item);
                    }
                }

            }

            WaitBack();

            #endregion

        }

        #region 工具栏响应
        private void btnEditLineDevice_Click(object sender, EventArgs e)
        {//跳转产线设备页面
            if (dgvDetails.CurrentRow != null)
            {
                ProductionLine p = dgvDetails.CurrentRow.DataBoundItem as ProductionLine;

                FrmMain.GetInstance().SwitchFormByModuleName("DeviceInfo", p);
            }
            else
            {
                ShowInfoMessageBox("提示", "先选择需要编辑设备信息的产线");
            }
        }

        private void toolBtnModify_Click(object sender, EventArgs e)
        {
            if (dgvDetails.CurrentRow != null)
            {
                ProductionLine p = dgvDetails.CurrentRow.DataBoundItem as ProductionLine;
                FrmAddOrEditProductionLine frm = new FrmAddOrEditProductionLine();
                frm.Tag = p;
                frm.Text = "修改产线信息";
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    p = frm.Tag as ProductionLine;
                    string sql = EntityHelper.GetUpdateSql(p);
                    string msg = string.Empty;

                    if (DALOpr.ExecuteNonQuery(sql, ref msg))
                    {
                        int idx = details.IndexOf(p);
                        details.RemoveAt(idx);
                        details.Insert(idx, p);

                        dgvDetails.SetRowSelect(idx);

                        ShowInfoMessageBox("提示", "修改成功");
                    }
                    else
                    {
                        ShowErrorMessageBox("修改失败", msg);
                    }
                }
            }
            else
            {
                ShowInfoMessageBox("提示", "先选择需要修改的记录");
            }
        }

        private void toolBtnDelete_Click(object sender, EventArgs e)
        {
            if (dgvDetails.CurrentRow != null)
            {
                ProductionLine p = dgvDetails.CurrentRow.DataBoundItem as ProductionLine;

                if (ShowConfirmDialog("请确认", "确定要删除该记录？\r\n" + p.ToString()) == System.Windows.Forms.DialogResult.OK)
                {
                    string sql = "delete from ProductionLine where id = " + p.ID;
                    string msg = string.Empty;

                    if (DALOpr.ExecuteNonQuery(sql, ref msg))
                    {
                        details.Remove(p);
                    }
                    else
                    {
                        ShowErrorMessageBox("删除失败", msg);
                    }
                }
            }
            else
            {
                ShowInfoMessageBox("提示", "先选择需要删除的记录");
            }
        }

        private void toolBtnAdd_Click(object sender, EventArgs e)
        {
            FrmAddOrEditProductionLine frm = new FrmAddOrEditProductionLine();
            frm.Text = "添加产品信息";
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ProductionLine p = frm.Tag as ProductionLine;
                string msg = string.Empty;

                if (details.Contains(p))
                {
                    ShowWarnningMessageBox("警告", "不允许添加同名生产线");
                    return;
                }

                if (DALOpr.Insert(p, ref msg))
                {
                    details.Add(p);
                    dgvDetails.SetRowSelect(details.Count - 1);
                    ShowInfoMessageBox("提示", "添加成功");
                }
                else
                {
                    ShowErrorMessageBox("添加失败", msg);
                }
            }
        }

        #endregion

        private void dgvDetails_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDetails.CurrentRow != null)
            {
                toolBtnModify.PerformClick();
            }
        }
    }
}
